package com.slx.sthq.controller;


import com.slx.sthq.config.MinioUtils;

import com.slx.sthq.config.properties.MinioProperties;
import com.slx.sthq.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import java.util.Calendar;

@RestController
@Api(value = "使用Minio上传文件", tags = "使用Minio上传文件")
@RequestMapping("/minio")
public class MinioUploadController {

    @Resource
    private MinioProperties minioProperties;
    @Resource
    private MinioUtils minioUtils;


    @PostMapping("/upload")
    @ApiOperation("上传")
    public R uploadFile(@RequestPart(value = "file") MultipartFile file) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String fileName = file.getOriginalFilename();
        String objectName = year + "/" + month + "/" + day + "/" + System.currentTimeMillis() + "/" + fileName;

        // 1. 上传文件
        minioUtils.putMultipartFile(minioProperties.getBucketName(), objectName, file);
        return R.ok(objectName);

    }


}
